local lishi = fk.CreateSkill {
  name = "emo__lishi",
  tags = {Skill.Quest},
}

Fk:loadTranslationTable{
  ["emo__lishi"] = "历世",
  [":emo__lishi"] = "使命技：出牌阶段，你可以弃置两张牌，从三张点数在两牌点数闭区间内的牌中选一张获得，若为本阶段首次发动，获得这三张。<br>"..
  "⬤　成功：使用13种点数的牌，获得<a href=':emo__zhuojian'>〖灼见〗</a>。<br>"..
  "⬤　失败：你使用牌后，没有手牌。",

  ["@[emonum]emo__lishi"] = "历世",
  ["#emo__lishi"] = "历世:弃置两张牌，获得 %arg 张点数在两牌点数闭区间内的牌",

  ["$emo__lishi1"] = "",
  ["$emo__lishi2"] = "",
}

lishi:addEffect("active", {
  anim_type = "drawcard",
  card_num = 2,
  target_num = 0,
  prompt = function (self, player)
    local n = 1
    if player:getMark("emo__lishi-phase") == 0 and player:usedSkillTimes(lishi.name, Player.HistoryPhase) == 0 then
      n = 3
    end
    return "#emo__lishi:::"..n
  end,
  can_use = function(self, player)
    return player:getQuestSkillState(lishi.name) == nil
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected < 2 and not player:prohibitDiscard(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local n1, n2 = Fk:getCardById(effect.cards[1]).number, Fk:getCardById(effect.cards[2]).number
    if n1 > n2 then n1, n2 = n2, n1 end
    local getall
    if player:usedSkillTimes(lishi.name, Player.HistoryPhase) <= 1 and player:getMark("emo__lishi-phase") == 0 then
      room:setPlayerMark(player, "emo__lishi-phase", 1)
      getall = true
    end
    room:throwCard(effect.cards, lishi.name, player, player)
    if player.dead then return end
    local ids = room:getCardsFromPileByRule(".|"..n1.."~"..n2, 3, "allPiles")
    if #ids == 0 then return false end
    if not getall then
      local cid = room:askToChooseCard(player, {
        target = player, skill_name = lishi.name, prompt = "#emo-getcard",
        flag = { card_data = { { lishi.name, ids } } }
      })
      ids = {cid}
    end
    room:obtainCard(player, ids, false, fk.ReasonJustMove, player, lishi.name)
  end,
})

lishi:addEffect(fk.CardUseFinished, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    if player:getQuestSkillState(lishi.name) ~= nil or not player:hasSkill(lishi.name) then return false end
    return target == player and player:isKongcheng()
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@[emonum]emo__lishi", 0)
    room:updateQuestSkillState(player, lishi.name, true)
  end,
})

Fk:addQmlMark{
  name = "emonum",
  how_to_show = function(name, value)
    if type(value) == "table" then
      return tostring(#value).."/13"
    end
    return " "
  end,
  qml_path = "packages/moepack/qml/EmoNumBox"
}

lishi:addEffect(fk.CardUsing, {
  anim_type = "big",
  can_trigger = function(self, event, target, player, data)
    if target ~= player or player:getQuestSkillState(lishi.name) ~= nil or not player:hasSkill(lishi.name) then return false end
    return #player:getTableMark("@[emonum]emo__lishi") == 13
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@[emonum]emo__lishi", 0)
    room:updateQuestSkillState(player, lishi.name)
    room:handleAddLoseSkills(player, "emo__zhuojian")
  end,
})

lishi:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    if target ~= player or player:getQuestSkillState(lishi.name) ~= nil or not player:hasSkill(lishi.name) then return false end
    local num = data.card.number
    return num > 0 and num < 14 and #player:getTableMark("@[emonum]emo__lishi") < 13
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@[emonum]emo__lishi")
    if table.insertIfNeed(mark, data.card.number) then
      room:setPlayerMark(player, "@[emonum]emo__lishi", mark)
    end
  end,
})

lishi:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@[emonum]emo__lishi", 0)
end)


return lishi
